<template>
  <div class="container">
    <div class="form-wrapper">
      <div class="header">注册</div>
      <el-form :model="registerForm" :rules="registerRules" ref="registerFormRef" class="signup-form">
        <el-form-item prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入身份证号" class="input-item"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="密码" class="input-item"></el-input>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input v-model="registerForm.phone" placeholder="电话号码(选填)" class="input-item"></el-input>
        </el-form-item>
        <el-button type="primary" class="btn" @click="handleRegister" :loading="isSubmitting">注册</el-button>
      </el-form>
      <div class="msg">
        已有账户?
        <router-link to="/login">登录</router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';
import { useRouter } from 'vue-router';

export default {
  setup() {
    const router = useRouter();

    const registerForm = ref({
      username: '',
      password: '',
      phone: '', // 可选字段
      role: 'user', // 默认角色
      lastLoginTime: null // 默认最后登录时间为null
    });

    const registerRules = ref({
      username: [
        { required: true, message: '请输入身份证号', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    });

    const isSubmitting = ref(false); // 控制按钮提交状态

    const registerFormRef = ref(null);

    const handleRegister = () => {
      registerFormRef.value.validate((valid) => {
        if (valid) {
          isSubmitting.value = true; // 启用加载状态
          // 发送注册请求到后端
          axios.post('http://localhost:8081/user/register', {
            username: registerForm.value.username,
            password: registerForm.value.password,
            phone: registerForm.value.phone,
            role: registerForm.value.role,
          })
              .then(response => {
                ElMessage({
                  message: '注册成功',
                  type: 'success',
                  duration: 2000
                });
                router.push({ name: 'Login' }); // 注册成功后跳转到登录页面
              })
              .catch(error => {
                console.error('注册失败:', error);
                ElMessage.error('注册失败，请重试');
              })
              .finally(() => {
                isSubmitting.value = false; // 关闭加载状态
              });
        } else {
          console.log('验证失败');
        }
      });
    };

    return {
      registerForm,
      registerRules,
      registerFormRef,
      isSubmitting,
      handleRegister
    };
  }
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
}

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: linear-gradient(to right, #808080, #a6c1ee);
}

.form-wrapper {
  background-color: #fff;
  width: 358px;
  height: 588px;
  border-radius: 15px;
  padding: 0 50px;
}

.header {
  font-size: 38px;
  font-weight: bold;
  text-align: center;
  line-height: 200px;
}

.input-item {
  display: block;
  width: 100%;
  margin-bottom: 10px;
  padding: 10px;
  border: none;
  border-bottom: 1px solid rgb(128, 125, 125);
  background: none;
  font-size: 15px;
  outline: none;
}

.input-item::placeholder {
  text-transform: uppercase;
}

.btn {
  text-align: center;
  padding: 10px;
  width: 100%;
  margin-top: 40px;
  background-image: linear-gradient(to right, #a6c1ee, #808080);
  color: #fff;
}

.msg {
  text-align: center;
  line-height: 88px;
}

a {
  text-decoration-line: none;
  color: #abc1ee;
}
</style>
